﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AccountingLibs;
using CustomSecurity;
using Lists;
using ListForms;
using GuiUtilities;
using BarCodeChuan;

namespace Portal
{
    public partial class FrmPortal : BasicForms.FrmBase
    {
        public FrmPortal()
        {
            InitializeComponent();

            #region Load last GUI'state
            if (LocalSettings.HeThong != null)
            {
                defaultLookAndFeel1.LookAndFeel.SkinName = LocalSettings.HeThong.SkinName;
            }
            #endregion

        }

        private void btnBanHangLe_Click(object sender, EventArgs e)
        {
            BanLe.FrmEditHoaDonBanLe frm = new BanLe.FrmEditHoaDonBanLe();
            frm.Show();
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            FrmCacCauHinh frm = new FrmCacCauHinh();
            frm.Show();

        }

        private void FrmPortal_Load(object sender, EventArgs e)
        {
            SetGUI();
            lblVersion.Text = string.Format("Phiên bản: {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
            try
            {
                #region ThietLap DangKy hoac lay ten may 
                LocalSettings.HeThong.HardwareID = CommonLib.GetMacAddressID();
                MayTinh mayTinh = DanhMucMayTinh.Instance.GetByHardwareID(LocalSettings.HeThong.HardwareID);
                if (mayTinh == null)
                {
                    mayTinh = DanhMucMayTinh.Instance.NewDoiTuong();
                    mayTinh.HardwareID = LocalSettings.HeThong.HardwareID;
                    mayTinh.Ten = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    mayTinh.GenSoMayBaseOnDB();
                    mayTinh.InsertToDB();
                }
                LocalSettings.HeThong.SoMay = mayTinh.GetSoMay();
                AccountingLibs.LocalSettings.HeThong.Save2File();
                #endregion

                if (LocalSettings.HeThong.AutoLogin)
                {
                    User.CurUser = User.Login(LocalSettings.HeThong.TenTruyCap, LocalSettings.HeThong.Password);
                    if (User.CurUser != null)
                    {
                        NhatKySuDung.Insert2DB("Đăng nhập", "hệ thống");
                    }
                }
                else
                {
                    LoginLogout();
                }
                SetGUI();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        private bool CheckLicence()
        {
            try
            {
                while (true)
                {
                    string inputSerial = LocalSettings.HeThong.SerialKey;

                    string actualCpuSerial = CommonLib.MaHoaMD5("Accounting" + CommonLib.MaHoaMD5("Accounting" + CommonLib.GetMacAddressID())) + "C";
                    if (inputSerial == actualCpuSerial)
                    {
                        return true;
                    }

                    string actualCompanyNameSerial = CommonLib.MaHoaMD5("Accounting" + Config.TenDoanhNghiep) + "B";
                    if (inputSerial == actualCompanyNameSerial)
                    {
                        //if (Config.TenDoanhNghiep.ToLower().StartsWith("demo") && CommonLib.GetSystemDateTime() > LocalSettings.HeThong.HanSuDung)
                        //{
                        //    MessageBox.Show("Sản phẩm demo đã hết hạn sử dụng");
                        //    return false;
                        //}
                        if (CommonLib.GetSystemDateTime() > LocalSettings.HeThong.HanSuDung)
                        {
                            MessageBox.Show("Sản phẩm đã hết hạn sử dụng");
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }

                    FrmSerial frmSerial = new FrmSerial();
                    if (frmSerial.ShowDialog() != DialogResult.OK)
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        public void LoginLogout()
        {
            try
            {
                if (User.CurUser == null)//login
                {
                    while (true)
                    {
                        try
                        {
                            FrmLogin frmLogin = new FrmLogin();
                            if (frmLogin.ShowDialog() == DialogResult.OK)
                            {
                                User.CurUser = User.Login(frmLogin.UserName, frmLogin.Password);
                                if (User.CurUser != null)
                                {
                                    NhatKySuDung.Insert2DB("Đăng nhập", "hệ thống");
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        catch (Exception loginEx)
                        {
                            MessageBox.Show(loginEx.Message, "Lỗi đăng nhập");
                        }
                    }
                    //if (!CheckLicence()) return;
                }
                else//logout
                {
                    foreach (Form frm in this.OwnedForms) frm.Close();
                    User.CurUser = null;
                    NhatKySuDung.Insert2DB("Đăng xuất", "hệ thống");
                }
                SetGUI();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi");
            }
        }
        private void SetGUI()
        {
            if (User.CurUser != null)
            {
                btnHeThong.Enabled = true;
                btnMaVach.Enabled = true;
                btnBanLe.Enabled = true;
                btnKinhDoanh.Enabled = true;
                lblDangNhap.Text = "Đăng xuất";
            }
            else
            {
                btnHeThong.Enabled = false;
                btnMaVach.Enabled = false;
                btnBanLe.Enabled = false;
                btnKinhDoanh.Enabled = false;
                lblDangNhap.Text = "Đăng nhập";
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            try
            {
                switch (keyData)
                {
                    case Keys.F1:
                        AccountingLibs.CommonLib.ShowHelp(this.Name);
                        break;

                    case Keys.Control | Keys.L:
                    case Keys.Control | Keys.D:
                        LoginLogout();
                        break;

                    //Some hide functions
                    case Keys.Control | Keys.Shift | Keys.Alt | Keys.H:
                        FrmHanSuDung frm = new FrmHanSuDung();
                        frm.ShowDialog();
                        break;

                    case Keys.Control | Keys.Shift | Keys.Alt | Keys.D:
                        FrmCauHinhFormDanhSachChungTu frmCauHinhFormDanhSachChungTu = new FrmCauHinhFormDanhSachChungTu();
                        frmCauHinhFormDanhSachChungTu.Show();
                        break;
                    case Keys.Control | Keys.Shift | Keys.Alt | Keys.F:
                        FrmCauHinhFormChungTu frmCauHinhFormChungTu = new FrmCauHinhFormChungTu();
                        frmCauHinhFormChungTu.Show();
                        break;
                    case Keys.Control | Keys.Shift | Keys.Alt | Keys.C:
                        FrmCauHinhCoSoDuLieu frmCauHinhCoSoDuLieu = new FrmCauHinhCoSoDuLieu();
                        frmCauHinhCoSoDuLieu.Show();
                        break;
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        private void btnHeThong_Click(object sender, EventArgs e)
        {
            FrmCacCauHinh frm = new FrmCacCauHinh();
            frm.Show();
        }

        private void btnMaVach_Click(object sender, EventArgs e)
        {
            FrmBarCode frm = new FrmBarCode();
            frm.Show();
        }

        private void btnBanLe_Click(object sender, EventArgs e)
        {
            GuiUtilities.FrmProgressBar.StartShowProgress();
            try
            {
                BanLe.FrmEditHoaDonBanLe frm = new BanLe.FrmEditHoaDonBanLe();
                frm.Show();
            }
            catch (Exception ex)
            {
            }
            GuiUtilities.FrmProgressBar.FinishShowProgress();
        }

        private void btnKinhDoanh_Click(object sender, EventArgs e)
        {

            GuiUtilities.FrmProgressBar.StartShowProgress();
            FrmKinhDoanh frm = new FrmKinhDoanh();
            frm.Show();

            GuiUtilities.FrmProgressBar.FinishShowProgress();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConfigDatabase_Click(object sender, EventArgs e)
        {
           
        }

        private void lblDangNhap_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LoginLogout();
            //if (User.CurUser != null)
            //{
                        
            //}
            //else
            //{

            //}
        }
    }
}
